#include "EDDemo.h"
#include "camera.h"
#include "matrix4.h"
Camera::Camera(void) : m_bActive(0)
{
	// Initialize all the camera's insides.
	m_Matrix.make_identity();
	m_Position.x = 0.0f;
    m_Position.z = 45.0f;
	m_Position.y = 15;
	m_Matrix.rotate_x_post(25);

	// Set teh cursor to 200, 200 (used in mouselook)
	SetCursorPos(200,200);
}

Camera::~Camera(void)
{
}
void Camera::update(void)
{   	
	if(GetAsyncKeyState(VK_LBUTTON))// & 0x8000)
	{
		if(!m_bActive)
			GetCursorPos(&m_centerPoint);
		// Move the camera forward, back, left, right.
		if(GetAsyncKeyState(VK_UP))
			m_Position = m_Position + (m_Matrix.axis_z * -1);// * 0.01;
		if(GetAsyncKeyState(VK_DOWN))
			m_Position += m_Matrix.axis_z;// * 0.01;
		if(GetAsyncKeyState(VK_LEFT))
			m_Position = m_Position + (m_Matrix.axis_x * -1);// * 0.01;
		if(GetAsyncKeyState(VK_RIGHT))
			m_Position += m_Matrix.axis_x;// * 0.01;

		// Rotate the camera based on the mouse movement.
		POINT cPos;
		float  sensitivity = 0.004f;
		GetCursorPos(&cPos);
		float xDiff = (cPos.x - m_centerPoint.x) * sensitivity;
		float yDiff = (cPos.y - m_centerPoint.y) * -sensitivity;		
		SetCursorPos(m_centerPoint.x,m_centerPoint.y);

		m_Matrix.rotate_y_pre(-xDiff);
		m_Matrix.rotate_x_post(yDiff);		
		m_bActive = true;
	}
	else
		m_bActive = false;
}

void Camera::applyCameraMatrix(void)
{
	// Apply the camera's matrix
	float4x4 invert(m_Matrix.xx, m_Matrix.xy, m_Matrix.xz, 0.0f,
				m_Matrix.yx, m_Matrix.yy, m_Matrix.yz, 0.0f,
				m_Matrix.zx, m_Matrix.zy, m_Matrix.zz, 0.0f,
				0.0f, 0.0f, 0.0f, 1.0f);
	invert.transpose();		// Invert the camera's matrix.
	glMultMatrixf((float *)invert.m);		// Mult the inverse of the camera's rotation matrix.
	glTranslatef(-m_Position.x, -m_Position.y, -m_Position.z);	// Translate by the negated camera's position.
}

